home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Applications
/
Python 1.3.3
/
Python 133 68K
/
Lib
/
fmt.py
< prev
next >
Wrap
Text File
|
1996-05-20
|
15KB
|
470 lines
# Text formatting abstractions
# Note -- this module is obsolete, it's too slow anyway
import string
import Para
# A formatter back-end object has one method that is called by the formatter:
# addpara(p), where p is a paragraph object. For example:
# Formatter back-end to do nothing at all with the paragraphs
class NullBackEnd:
#
def __init__(self):
pass
#
def addpara(self, p):
pass
#
def bgn_anchor(self, id):
pass
#
def end_anchor(self, id):
pass
# Formatter back-end to collect the paragraphs in a list
class SavingBackEnd(NullBackEnd):
#
def __init__(self):
self.paralist = []
#
def addpara(self, p):
self.paralist.append(p)
#
def hitcheck(self, h, v):
hits = []
for p in self.paralist:
if p.top <= v <= p.bottom:
for id in p.hitcheck(h, v):
if id not in hits:
hits.append(id)
return hits
#
def extract(self):
text = ''
for p in self.paralist:
text = text + (p.extract())
return text
#
def extractpart(self, long1, long2):
if long1 > long2: long1, long2 = long2, long1
para1, pos1 = long1
para2, pos2 = long2
text = ''
while para1 < para2:
ptext = self.paralist[para1].extract()
text = text + ptext[pos1:]
pos1 = 0
para1 = para1 + 1
ptext = self.paralist[para2].extract()
return text + ptext[pos1:pos2]
#
def whereis(self, d, h, v):
total = 0
for i in range(len(self.paralist)):
p = self.paralist[i]
result = p.whereis(d, h, v)
if result <> None:
return i, result
return None
#
def roundtowords(self, long1, long2):
i, offset = long1
text = self.paralist[i].extract()
while offset > 0 and text[offset-1] <> ' ': offset = offset-1
long1 = i, offset
#
i, offset = long2
text = self.paralist[i].extract()
n = len(text)
while offset < n-1 and text[offset] <> ' ': offset = offset+1
long2 = i, offset
#
return long1, long2
#
def roundtoparagraphs(self, long1, long2):
long1 = long1[0], 0
long2 = long2[0], len(self.paralist[long2[0]].extract())
return long1, long2
# Formatter back-end to send the text directly to the drawing object
class WritingBackEnd(NullBackEnd):
#
def __init__(self, d, width):
self.d = d
self.width = width
self.lineno = 0
#
def addpara(self, p):
self.lineno = p.render(self.d, 0, self.lineno, self.width)
# A formatter receives a stream of formatting instructions and assembles
# these into a stream of paragraphs on to a back-end. The assembly is
# parametrized by a text measurement object, which must match the output
# operations of the back-end. The back-end is responsible for splitting
# paragraphs up in lines of a given maximum width. (This is done because
# in a windowing environment, when the window size changes, there is no
# need to redo the assembly into paragraphs, but the splitting into lines
# must be done taking the new window size into account.)
# Formatter base class. Initialize it with a text measurement object,
# which is used for text measurements, and a back-end object,
# which receives the completed paragraphs. The formatting methods are:
# setfont(font)
# setleftindent(nspaces)
# setjust(type) where type is 'l', 'c', 'r', or 'lr'
# flush()
# vspace(nlines)
# needvspace(nlines)
# addword(word, nspaces)
class BaseFormatter:
#
def __init__(self, d, b):
# Drawing object used for text measurements
self.d = d
#
# BackEnd object receiving completed paragraphs
self.b = b
#
# Parameters of the formatting model
self.leftindent = 0
self.just = 'l'
self.font = None
self.blanklines = 0
#
# Parameters derived from the current font
self.space = d.textwidth(' ')
self.line = d.lineheight()
self.ascent = d.baseline()
self.descent = self.line - self.ascent
#
# Parameter derived from the default font
self.n_space = self.space
#
# Current paragraph being built
self.para = None
self.nospace = 1
#
# Font to set on the next word
self.nextfont = None
#
def newpara(self):
return Para.Para()
#
def setfont(self, font):
if font == None: return
self.font = self.nextfont = font
d = self.d
d.setfont(font)
self.space = d.textwidth(' ')
self.line = d.lineheight()
self.ascent = d.baseline()
self.descent = self.line - self.ascent
#
def setleftindent(self, nspaces):
self.leftindent = int(self.n_space * nspaces)
if self.para:
hang = self.leftindent - self.para.indent_left
if hang > 0 and self.para.getlength() <= hang:
self.para.makehangingtag(hang)
self.nospace = 1
else:
self.flush()
#
def setrightindent(self, nspaces):
self.rightindent = int(self.n_space * nspaces)
if self.para:
self.para.indent_right = self.rightindent
self.flush()
#
def setjust(self, just):
self.just = just
if self.para:
self.para.just = self.just
#
def flush(self):
if self.para:
self.b.addpara(self.para)
self.para = None
if self.font <> None:
self.d.setfont(self.font)
self.nospace = 1
#
def vspace(self, nlines):
self.flush()
if nlines > 0:
self.para = self.newpara()
tuple = None, '', 0, 0, 0, int(nlines*self.line), 0
self.para.words.append(tuple)
self.flush()
self.blanklines = self.blanklines + nlines
#
def needvspace(self, nlines):
self.flush() # Just to be sure
if nlines > self.blanklines:
self.vspace(nlines - self.blanklines)
#
def addword(self, text, space):
if self.nospace and not text:
return
self.nospace = 0
self.blanklines = 0
if not self.para:
self.para = self.newpara()
self.para.indent_left = self.leftindent
self.para.just = self.just
self.nextfont = self.font
space = int(space * self.space)
self.para.words.append(self.nextfont, text, \
self.d.textwidth(text), space, space, \
self.ascent, self.descent)
self.nextfont = None
#
def bgn_anchor(self, id):
if not self.para:
self.nospace = 0
self.addword('', 0)
self.para.bgn_anchor(id)
#
def end_anchor(self, id):
if not self.para:
self.nospace = 0
self.addword('', 0)
self.para.end_anchor(id)
# Measuring object for measuring text as viewed on a tty
class NullMeasurer:
#
def __init__(self):
pass
#
def setfont(self, font):